import {
    formatDate
} from '/js/util.js'
let dialog = (list) => {
    console.log('dialog',list)
    let dialogs = document.createElement('div')
    dialogs.className = 'message-dialog1'
    let top = `
        <div style='display:flex;align-items:flex-start;justify-content: space-between;
        padding: 15px;'>
            <img src="${list.teacherimg}" width='36' height='36'/>
            <i class='iconfont icon-chahao close-star' style='cursor:pointer;font-size:11px;color:#C2C2C2'></i>
        </div>
        <div style='padding-left:15px;color: #3C444B;font-size:16px'>评论${list.name}老师</div>
        <div style='font-size: 12px;color: #717A82;margin-top:3px;margin-left:15px'>帮助学生选择合适的导师</div>
        <div style='display:flex;margin-left:15px;margin-top:7px' class='pinfen-wrap'>
            <i data-index='${1}' class='iconfont icon-B-pingfen pinfen' style='font-size:16px;'></i>
            <i data-index='${2}' class='iconfont icon-B-pingfen pinfen' style='font-size:16px;margin-left:5px;'></i>
            <i data-index='${3}' class='iconfont icon-B-pingfen pinfen' style='font-size:16px;margin-left:5px;'></i>
            <i data-index='${4}' class='iconfont icon-B-pingfen pinfen' style='font-size:16px;margin-left:5px;'></i>
            <i data-index='${5}'class='iconfont icon-B-pingfen pinfen' style='font-size:16px;margin-left:5px;'></i>
        </div>
        <div class='content1' style='overflow-y: scroll;outline: 0;border: 1px solid #F0F0F0;height:51px;max-height:51px;margin-left:15px;margin-right:15px;margin-top:6px;
        border-radius: 3px;padding:4px;color:#B6BDC2' contenteditable="true">
            描述你的学习经历 这个评论的内容是20到60个单词
        </div>
        <div class='button' id='button' style='cursor:pointer;width:80%;margin:10px auto;border:1px solid #4580FE;display:flex;align-items:center;background: #4580FE;border-radius:6px;color:white;font-size:12px;justify-content:center;height:28px'>Post review</div>
    `
    dialogs.innerHTML = top
    dialogs.style.display = 'none'
    $('.buy')[0] && $('.buy')[0].appendChild(dialogs)

}

let dialogListener = (list) => {
    let userid = list.userid
    let wrap = $('.pinfen-wrap')[0]
    let button = $('.button')[0]
    let close = $('.close-star')[0]
    wrap.addEventListener('mousedown', (e) => {
        let children = wrap.children
        Array.from(children).forEach(item => {
            if (item.dataset.index <= e.target.dataset.index) {
                item.className = 'iconfont icon-B-pingfen pinfen pifenactive'
            } else {
                item.className = 'iconfont icon-B-pingfen pinfen'
            }
        })
    })
    button.addEventListener('click', () => {
        let star = 0
        let children = wrap.children
        star = Array.from(children).filter(item => {
            return item.className.includes('pifenactive')
        }).length


        let params = {
            teacherid: userid,
            studentid: JSON.parse(localStorage.getItem('user')).user_id,
            superclassid: '',
            name: JSON.parse(localStorage.getItem('user')).user_name,
            evaluate: $('.content1')[0].innerText,
            star,
            createtime: formatDate(new Date())
        }

        let get = async () => {
            let res = await ajax("user_comments_teacher", params, 'post');
            console.log(res)
            if(res?.code === 200){
                Dreamer.success('提交成功')
            }else{
                Dreamer.error('提交失败')
            }
        }

        get()
    })
    close.addEventListener('click', (e) => {
        $('.message-dialog1').hide()
    })
}

export{
    dialog,
    dialogListener
}